import axios from 'axios';
import { ElMessage } from 'element-plus';
import { getToken } from './setTokenUtils';
import router from '../router';

/**
 *  展示地址  https://slwl-geteway.itheima.net
 *  开发地址  https://slwl-api.itheima.net
 * */ 

const request = axios.create({
  baseURL: 'https://slwl-geteway.itheima.net',
  timeout: 5000,
});

const token = getToken();
// const token = LoginSetToken.Token;
//请求
request.interceptors.request.use(
  (config) => {
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    console.log('@@@@ --------------------');

    console.log(error);
  }
);

//响应
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    if (error.response.status === 401) {
      //清除piain里token //清除本地的token
      // LoginSetToken.DELTOKEN();
      router.push('/login');
    }
    ElMessage.error(JSON.parse(error.request.response).msg);
    return Promise.reject(error);
  }
);

export default request;
